import roles from '../../jsons/roles.json'
import {
    getList,
    deptList,
    memberList
} from '@/api/approve/approve'

import departments from '../../jsons/departments.json'
import employees from '../../jsons/employees.json'

export default {
    data() {
        return {
            page: {
                pageSize: 10,
                currentPage: 1,
                total: 0
            },
            visibleDialog: false,
            searchVal: "",
            activeName: "1",
            departments: {},
            roles: [],
            memberList: [],
            deptName: '',
            memberTotal: ''
        }
    },
    methods: {
        getRoleList() {
            // this.roles = roles.data.list
            getList(
                this.page.currentPage,
                this.page.pageSize,
                this.treeDeptId
            ).then(res => {
                const data = res.data.data[1].children
                let newData = JSON.parse(JSON.stringify(data).replace(/roleName/g, "label"));
                this.roles = newData
            })
        },
        getMemberList() {
            memberList(
                    this.page.currentPage,
                    this.page.pageSize
                ).then(res => {
                    const data = res.data.data.records
                    const total = res.data.data.total
                    let newData = JSON.parse(JSON.stringify(data).replace(/name/g, "label"));
                    this.memberList = newData
                    this.memberTotal = total
                })
        },
        getDepartmentList(parentId = 0) {
            deptList(
                this.page.currentPage,
                this.page.pageSize,
            ).then(res => {
                let data = res.data.data
                this.deptName = res.data.data[0].deptName
                let departList = JSON.parse(JSON.stringify(data).replace(/deptName/g, "label"));
                this.departments = departList
            })
        },
        getDebounceData(event, type = 1) {
            this.$func.debounce(function() {
                if (event.target.value) {
                    if (type == 1) {
                        this.departments.childDepartments = [];
                        this.departments.employees = employees.data.list
                    } else {
                        getList(
                            this.page.currentPage,
                            this.page.pageSize,
                            this.treeDeptId
                        ).then(res => {
                            const data = res.data.data[1].children
                            let newData = JSON.parse(JSON.stringify(data).replace(/deptName/g, "label"));
                            this.roles = newData
                        })
                    }
                } else {
                    type == 1 ? this.getDepartmentList() : this.getRoleList();
                }
            }.bind(this))()
        },
    }
}
